package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ImportExcelInterface;
import com.koron.css2.common.dto.HeaderInfoDto;
import com.koron.css2.newProjectCenter.bean.PjMaterialBean;
import com.koron.css2.newProjectCenter.mapper.PjMaterialMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 工程材料导入
 */
public class PjMaterialImport implements ImportExcelInterface {

    private static Logger logger = LoggerFactory.getLogger(PjMaterialImport.class);

    // 金额正则匹配
    private static final String REGEX = "^[0-9]+(\\.[0-9]{1,2})?$";

    @Override
    public MessageBean<?> importExel(SessionFactory factory, UserInfoBean userInfo, RequestBean req, List<Map<String, String>> data, MultipartFile file) {
        try {
            MessageBean message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "工程材料导入成功", String.class);
            List<PjMaterialBean> pjMaterialList = JsonUtils.objectToList(data, PjMaterialBean.class);
            PjMaterialMapper pjMaterialMapper = factory.getMapper(PjMaterialMapper.class);
            // 将材料编号转成 set 集合
            Set<String> pjMaterialNoSet = pjMaterialList.stream().map(t -> t.getMaterialNo()).collect(Collectors.toSet());
            StringBuilder stringBuilder = new StringBuilder("");
            for (int i = 0; i < pjMaterialList.size(); i++){
                PjMaterialBean pjMaterial = pjMaterialList.get(i);
                if (StringUtils.isBlank(pjMaterial.getMaterialName())){
                    stringBuilder.append("第" + (i + 1) + "行：材料名称为空 <br/>");
                }
                if (StringUtils.isEmpty(pjMaterial.getPriceStr())){
                    stringBuilder.append("第" + (i + 1) + "行：材料价格为空 <br/>");
                }
                if (StringUtils.isNotEmpty(pjMaterial.getPriceStr())){
                    if (!pjMaterial.getPriceStr().matches(REGEX)){
                        stringBuilder.append("第" + (i + 1) + "行：材料价格只能为数字 <br/>");
                    }else {
                        pjMaterial.setPrice(Double.parseDouble(pjMaterial.getPriceStr()));
                    }
                }
                // 验证是否存在相同的材料编号
                if (StringUtils.isNotBlank(pjMaterial.getMaterialNo())){
                    PjMaterialBean oldPjMaterial = pjMaterialMapper.selectMaterial(pjMaterial.getMaterialNo());
                    if (oldPjMaterial != null){
                        stringBuilder.append("第" + (i + 1) + "行：系统中已存在" + pjMaterial.getMaterialNo() + "编号的材料 <br/>");
                    }
                }
                if (StringUtils.isNotBlank(pjMaterial.getStatusName())){
                    pjMaterial.setStatus(Objects.equals("启用", pjMaterial.getStatusName()) ? 1 : 0);
                }else {
                    pjMaterial.setStatus(1);
                }
                pjMaterial.setCreateInfo(userInfo);
            }
            // 判断是否存在重复的材料编号
            if (pjMaterialNoSet.size() != pjMaterialList.size()){
                stringBuilder.append("存在重复的材料编号<br/>");
            }
            // 存在错误信息
            if (stringBuilder.toString().length() > 0){
                MessageBean errMsg = MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程材料导入失败", String.class);
                // 返回错误信息
                errMsg.setData(stringBuilder.toString());
                return errMsg;
            }
            // 批量插入导入的数据
            pjMaterialMapper.batchAddPjMaterial(pjMaterialList);
            return message;
        }catch (Exception exception){
            logger.error("工程材料导入数据获取失败：" + exception);
        }
        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程材料导入失败", String.class);
    }

    @Override
    public List<HeaderInfoDto> getHeaderInfo(UserInfoBean userInfo) {
        List<HeaderInfoDto> headerInfos = new ArrayList<HeaderInfoDto>();
        headerInfos.add(new HeaderInfoDto("材料名称", true, "materialName"));
        headerInfos.add(new HeaderInfoDto("材料编号", true, "materialNo"));
        headerInfos.add(new HeaderInfoDto("材料规格", true, "specs"));
        headerInfos.add(new HeaderInfoDto("单位", true, "unit"));
        headerInfos.add(new HeaderInfoDto("价格", true, "priceStr"));
        headerInfos.add(new HeaderInfoDto("状态", true, "statusName"));
        headerInfos.add(new HeaderInfoDto("描述", false, "remarks"));
        return headerInfos;
    }

    @Override
    public boolean isCheckHeader() {
        return true;
    }
}
